/*
 * @Autor: Shihh
 * @Description: 
 * @Version: 1.0
 * @Date: 2020-05-08 17:48:26
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2020-05-21 16:45:26
 */
const Sequelize = require('sequelize')

module.exports = (sequelize, DataTypes) => {
  const tags = sequelize.import('./m_tags',require('./m_tags'))  
    class m_group extends Sequelize.Model {

        /**
         * @author: Shihh
         * @description: 获取所有分组
         * @param {type} 
         * @return: 
         */
        static async getGroupList() {
            let condition = {
                //返回的字段
                attributes: ['code', 'group_name'],
            }
            return await this.findAndCountAll(condition)
        }

        /**
         * @author: Shihh
         * @description: 分页获取分组  带出tagname
         * @param {type} 
         * @return: 
         */
        static async getTableGroupList(pars) {
            var [offset = 1, limit = 1] = [pars.pagesize * (pars.page - 1), pars.pagesize]
            let condition = {
                include: [{ 
                  model: (tags), 
                  as: 't', 
                  attributes: [
                    [sequelize.fn('count', sequelize.col('t.group')), 'num']
                  ], 
                  required:false
                }],
                raw: true, 
                offset: offset,
                raw: true,
                group:'tags_group.code',
                subQuery:false,
                limit: parseInt(limit)
              }
            return this.findAll(condition);
        }
        /**
         * @author: Shihh
         * @description: 获取分组总数
         * @param {type} 
         * @return: 
         */
        static getGroupCount() {
            return this.count()
        }

        /**
         * @author: Shihh
         * @description: 条件获取某个分组
         * @param {type} 
         * @return: 
         */
        static async getOneGroup(paras) {
            let where = {}
            let condition = {
                attributes: ['code', 'group_name'],
            }
            if (paras.code != undefined && paras.code != null) {
                where.code = paras.code
            }
            if (paras.group_name != undefined && paras.group_name != null) {
                where.group_name = paras.group_name
            }
            condition.where = where
            let data = await this.findOne(condition)
            return data
        }

        /**
         * @author: Shihh
         * @description: 更新分组
         * @param {type} 
         * @return: 
         */        
        static async updateGroup(paras) {
            return await this.update({
                group_name: paras.group_name,
            }, {
                'where': {
                    'code': paras.code
                }
            }).then(re => {
                return true
            }).catch(err => {
                return false
            })
        }

    }

    m_group.init({
        code: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
        group_name: { type: DataTypes.STRING(45) },
    }, {
        sequelize,
        modelName: 'tags_group',
        freezeTableName: true,
        timestamps: false
    });
    m_group.hasMany(tags, { as: 't', foreignKey: 'group', targetKey: 'code' });

    return m_group;
}